package com.wswy.wyjk.ui.main

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentPagerAdapter
import android.support.v4.content.ContextCompat
import android.support.v4.view.ViewPager
import com.blankj.utilcode.util.SizeUtils
import com.raft.framework.base.RFBaseActivity
import com.raft.framework.utils.UICompat
import com.raft.framework.utils.UiUtils

import com.wswy.wyjk.R
import com.wswy.wyjk.core.App
import com.wswy.wyjk.model.PracticeContext
import com.wswy.wyjk.ui.common.Router
import com.wswy.wyjk.ui.main.presenter.MainPresenter
import com.wswy.wyjk.utils.ActivityUtils

import com.wswy.wyjk.utils.Util
import kotlinx.android.synthetic.main.activity_main.*

import net.lucode.hackware.magicindicator.ViewPagerHelper
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView

class MainActivity : RFBaseActivity() {

    private lateinit var titleNavigator: CommonNavigator
    val mainP = MainPresenter(this)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        UiUtils.setStatusBarLight(this, true)

        App.getPracticeContext()?.let {
            initViews(it)
        } ?: kotlin.run {
            finish()
            return
        }

        rl_bottom.setOnClickListener {

            Router.start(this, Router.CHOOSE_TYPE)

        }

        App.getPracticeContextLiveData().observeForever {
            it?.let {
                initViews(it)
            }
        }
        img_app_icon.setBackgroundResource(
            Util.getMetaDataInt(
                this,
                "app_icon"
            )
        )

        //main presenter
        // mainP.init()
    }

    private fun initViews(practiceContext: PracticeContext) {
        titleNavigator = CommonNavigator(this).apply {
            isSkimOver = true
        }

        val datas = if (practiceContext.haveSubject()) arrayOf("科目一", "科目四") else arrayOf("资格证")


        titleNavigator.adapter = object : CommonNavigatorAdapter() {

            override fun getCount(): Int {
                return datas.size
            }

            override fun getTitleView(context: Context, index: Int): IPagerTitleView {
                val pagerTitleView = ColorTransitionPagerTitleView(context).apply {
                    setPadding(SizeUtils.dp2px(12f), 0, SizeUtils.dp2px(12f), 0)
                    text = datas[index]
                    textSize = 20f
                    selectedColor =
                        ContextCompat.getColor(applicationContext, R.color.textColorPrimary)
                    normalColor =
                        ContextCompat.getColor(applicationContext, R.color.textColorSecondary)
                }

                pagerTitleView.setOnClickListener { view_pager.currentItem = index }
                return pagerTitleView
            }

            override fun getIndicator(context: Context): IPagerIndicator? {
                val linePagerIndicator = LinePagerIndicator(context)
                linePagerIndicator.mode = LinePagerIndicator.MODE_WRAP_CONTENT
                linePagerIndicator.lineHeight = UICompat.dp2px(1f).toFloat()
                linePagerIndicator.setColors(
                    ContextCompat.getColor(
                        applicationContext,
                        R.color.textColorPrimary
                    )
                )
                return linePagerIndicator
            }
        }

        magic_indicator.navigator = titleNavigator

        view_pager.offscreenPageLimit = datas.size


        view_pager.adapter = object : FragmentPagerAdapter(supportFragmentManager) {
            override fun getItem(position: Int): Fragment {
                return SubjectFragment.newInstance(position)
            }

            override fun getCount(): Int = datas.size

            override fun getPageTitle(position: Int): CharSequence? {
                return datas[position]
            }
        }

        view_pager.clearOnPageChangeListeners()
        view_pager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {}

            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
            }

            override fun onPageSelected(position: Int) {
                changeSubjectType(practiceContext, position)
            }
        })
        ViewPagerHelper.bind(magic_indicator, view_pager)

        tv_curr_tiku.text =
            getString(R.string.current_practice_type, practiceContext.licenseType.title)
        tv_current_city.text = practiceContext.cityName

        ll_setting.setOnClickListener {
            startActivity(Intent(this, MineActivity::class.java))
        }
        changeSubjectType(practiceContext, view_pager.currentItem)
    }

    fun changeSubjectType(practiceContext: PracticeContext, p: Int) {
        if (practiceContext.haveSubject()) {
            MineActivity.subjectType = p
        } else {
            MineActivity.subjectType = 2
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        App.getPracticeContextLiveData().removeObservers(this)
    }

    override fun onBackPressed() {
        if (ActivityUtils.isExit()) {
            super.onBackPressed()
            return
        }
    }
}
